<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handleAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table row-key="id" border :data="permissionList">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <!-- Attention：当type为1时是一级权限 type为2二级权限 没有三级权限 -->
              <el-button v-if="row.type===1" size="small" type="text" @click="handleAdd(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="toEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="formData.id ? '编辑权限' : '添加权限'" @close="handleClose">
      <!-- 表单内容 -->
      <el-form ref="myForm" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-value="0"
            inactive-value="1"
            active-text="启用"
            inactive-text="禁用"
            active-color="#13ce66"
            inactive-color="#eee"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="submitFormData">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { translateListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      permissionList: [],
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        // 是required哦
        name: [{ required: true, trigger: ['blur', 'change'], message: '名称不能为空' }],
        code: [{ required: true, trigger: ['blur', 'change'], message: '权限标识不能为空' }]
      }
    }
  },

  created() {
    this.getPermissionList()
  },
  methods: {
    handleClose() {
      this.$refs.myForm.resetFields()
      this.formData = this.$options.data().formData
      this.showDialog = false
    },
    // 查看回显 修改权限
    async toEdit(id) {
      this.showDialog = true
      const ret = await reqGetPermissionDetail(id)
      if (ret.success) {
        // 回填的数据中包含了formData.id 回填前没有定义id的值
        this.formData = ret.data
      }
    },
    // 删除权限
    handleDelete(id) {
      this.$confirm('确认删除吗？', '温馨提示').then(async() => {
        const ret = await reqDelPermission(id)
        if (ret.success) {
          this.$message.success('删除权限成功')
          this.getPermissionList()
        } else {
          this.$message.error('删除权限失败')
        }
      }).catch(e => {
        if (e !== 'cancel') {
          this.$message.error('删除权限失败')
        }
      })
    },
    // 提交表单
    submitFormData() {
      this.$refs.myForm.validate(async valid => {
        if (!valid) return
        // console.log('*****' + this.formData.id)
        // 修改权限
        if (this.formData.id) {
          // console.log('*****' + this.formData.id) // id是1
          // this.formData.id id哪里来的呢？
          const ret = await reqUpdatePermission(this.formData)
          if (ret.success) {
            this.$message.success('编辑权限成功')
            this.getPermissionList()
            this.handleClose()
          } else {
            this.$message.error('编辑权限失败')
          }
        } else {
          // 添加权限
          const ret = await reqAddPermission(this.formData)
          // console.log(this.formData)
          if (ret.code === 10000) {
            this.$message.success('添加权限点成功')
            this.getPermissionList()
            this.handleClose()
          } else {
            this.$message.error('添加权限失败')
          }
        }
      })
    },
    handleAdd(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async  getPermissionList () {
      try {
        const ret = await reqGetPermissionList()
        if (ret.code === 10000) {
          // 数组转化传成树形数据结构的方法
          this.permissionList = translateListToTreeData(ret.data, '0')
        }
      } catch {
        this.$message.error('获取权限列表失败')
      }
    }
  }
}
</script>
